package com.qyhl.module_activities.act.detail;

import android.text.TextUtils;

import com.google.gson.Gson;
import com.qyhl.module_activities.R;
import com.qyhl.module_activities.app.ActivitiesApplication;
import com.qyhl.module_activities.common.ActUrl;
import com.qyhl.webtv.basiclib.utils.ContextUtilts;
import com.qyhl.webtv.basiclib.utils.NetUtil;
import com.qyhl.webtv.basiclib.utils.network.EasyHttp;
import com.qyhl.webtv.basiclib.utils.network.callback.SimpleCallBack;
import com.qyhl.webtv.basiclib.utils.network.exception.ApiException;
import com.qyhl.webtv.basiclib.utils.network.model.ApiResult;
import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.commonlib.entity.act.ActDetailsBean;

public class ActNewDetailsModel implements ActNewDetailsContract.ActDetailsModel {
    private ActNewDetailsContract.ActDetailsPresenter presenter;

    public ActNewDetailsModel(ActNewDetailsContract.ActDetailsPresenter presenter) {
        this.presenter = presenter;
    }

    @Override
    public void getActDetails(int actId, final int type) {
        if (NetUtil.isNetworkAvalible(ActivitiesApplication.getApp())) {
            String userName = CommonUtils.getInstance().getUserName();

            EasyHttp.get(ActUrl.ACTIVITY_DETAILS)
                    .params("actId", String.valueOf(actId))
                    .params("phone", TextUtils.isEmpty(userName) ? "1" : userName)
                    .execute(new SimpleCallBack<ActDetailsBean>() {
                        @Override
                        public void onError(ApiException e) {
                            if (type == 0) {
                                presenter.onError(1, ContextUtilts.getInstance().getmContext().getString(R.string.error_str));
                            } else {
                                presenter.onError(2, ContextUtilts.getInstance().getmContext().getString(R.string.error_str));
                            }
                        }

                        @Override
                        public void onSuccess(ActDetailsBean actDetailsBean) {
                            presenter.onSuccess(actDetailsBean);
                        }
                    });

        } else {
            if (type == 0) {
                presenter.onError(1, "无网络连接，请检查您的网络...");
            } else {
                presenter.onError(2, "无网络连接，请检查您的网络...");
            }
        }

    }

    @Override
    public void checkSign(String actId) {
        String userName = CommonUtils.getInstance().getUserName();

        EasyHttp.post(ActUrl.ACTIVITY_IS_SIGN)
                .params("userName", userName)
                .params("actId", actId)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onError(ApiException e) {
                        presenter.checkError(-1);
                    }

                    @Override
                    public void onSuccess(String s) {
                        ApiResult apiResult = new Gson().fromJson(s,ApiResult.class);
                        if (apiResult.getCode() == 200) {
                            presenter.checkSuccess();
                        } else {
                            presenter.checkError(apiResult.getCode());
                        }
                    }
                });
    }

    @Override
    public void colleciton(String actId, String flag) {
        if (NetUtil.isNetworkAvalible(ActivitiesApplication.getApp())) {
            String userName = CommonUtils.getInstance().getUserName();

            EasyHttp.get(ActUrl.ACTIVITY_COLLECTION)
                    .params("phone", userName)
                    .params("actId", actId)
                    .params("flag", flag)
                    .execute(new SimpleCallBack<String>() {
                        @Override
                        public void onError(ApiException e) {
                            presenter.collecitonError("网络繁忙，请稍后再试");
                        }

                        @Override
                        public void onSuccess(String s) {
                            ApiResult apiResult = new Gson().fromJson(s,ApiResult.class);
                            if (apiResult.getCode() == 200) {
                                presenter.collecitonSuccess();
                            } else {
                                presenter.collecitonError(apiResult.getCode() + "--" + apiResult.getMessage());
                            }
                        }
                    });

        } else {
            presenter.collecitonError("无网络连接，请检查您的网络...");
        }

    }
}
